<template>
  <div class="changepwd-content">
    <div class="changepwd-addinput">
      <p>修改密码</p>
      <div class="changepwd">
        <p>原密码:<el-input v-model="oldpwd" clearable> </el-input></p>
        <p>新密码:<el-input v-model="newpwd" show-password></el-input><br /></p>
        <p>
          确认新密码:<el-input
            class="addpwd"
            v-model="confirmpwd"
            show-password
          ></el-input>
        </p>

        <el-button type="success" @click="determineclick">确定</el-button>
        <el-button type="primary" @click="resetclick">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { inspectpwd, changepwd } from "@/apis/users";
export default {
  data() {
    return {
      oldpwd: "",
      newpwd: "",
      confirmpwd: "",
      nice: false,
      no: false,
    };
  },
  methods: {
    // 确认修改密码时校验旧密码以及修改旧密码接口联调
    determineclick() {
      let loginid = JSON.parse(window.localStorage.getItem("user"));
      //验证密码接口
      inspectpwd({ oldPwd: this.oldpwd, id: loginid }).then((res) => {
        if (res.data.code == 0) {
          this.nice = true;
          //修改密码接口
          if (this.newpwd == this.confirmpwd) {//判断当两次密码一致时才发送修改密码接口请求
            changepwd({ newPwd: this.newpwd, id: loginid }).then((res) =>
              console.log(res.data)
            );
            this.$message({
              message: res.data.msg,
              type: "success",
            });
          }else{//当两次不密码一致时提醒
             this.$message({
            message: "两次密码不一致哦，请重新输入",
            type: "warning",
          });
          }
        } else {
          this.$message({//这是旧密码的else,接口请求错误是提醒旧密码错误
            message: "旧密码不正确哦",
            type: "warning",
          });
        }
      });
    },
    // 重置按钮
    resetclick() {
      this.oldpwd='',this.newpwd='',this.confirmpwd=''
    },
  },
};
</script>

<style lang="less" scoped>
.changepwd-addinput > p {
  font-size: 20px;
  border-bottom: 1px solid #ccc;
  margin-bottom: 30px;
}
.changepwd > p {
  text-indent: 40px;
}
.el-input {
  width: 40%;
  margin-bottom: 20px;
}
.el-button {
  margin-left: 90px;
}
.addpwd {
  text-indent: 10px;
}
</style>